package com.aiiter.house.service.impl;

import java.util.Date;
import java.util.List;

import com.aiiter.common.utils.uuid.IdUtils;
import com.aiiter.house.domain.HouseProperty;
import com.aiiter.house.mapper.HousePropertyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aiiter.house.mapper.HouseRoomInformationMapper;
import com.aiiter.house.domain.HouseRoomInformation;
import com.aiiter.house.service.IHouseRoomInformationService;

import javax.annotation.Resource;

/**
 * 存储房间的基本信息，包括与房产的关联Service业务层处理
 * 
 * @author yaocheng
 * @date 2024-08-13
 */
@Service
public class HouseRoomInformationServiceImpl implements IHouseRoomInformationService 
{
    @Resource
    private HouseRoomInformationMapper houseRoomInformationMapper;
    @Autowired
    private HousePropertyMapper housePropertyMapper;
    /**
     * 查询存储房间的基本信息，包括与房产的关联
     * 
     * @param roomId 存储房间的基本信息，包括与房产的关联主键
     * @return 存储房间的基本信息，包括与房产的关联
     */
    @Override
    public HouseRoomInformation selectHouseRoomInformationByRoomId(Long roomId)
    {
        HouseRoomInformation houseRoomInformation = houseRoomInformationMapper.selectHouseRoomInformationByRoomId(roomId);
//        houseRoomInformation.setPropertyId(selectPropertyName(houseRoomInformation.getPropertyId()));
        return houseRoomInformation;
    }

    @Override
    public String importUser(List<HouseRoomInformation> roomInformationList, Boolean isUpdateSupport, String operName) {
        StringBuilder message = new StringBuilder();

        for (HouseRoomInformation roomInfo : roomInformationList) {
            if (roomInfo == null || roomInfo.getRoomNumber() == null) {
                message.append("行 " + (roomInformationList.indexOf(roomInfo) + 1) + ": 数据无效\n");
                continue;
            }

            // 尝试插入房间信息
            try {
                if (isUpdateSupport) {
                    // 更新逻辑，如果房间ID存在则更新
                    if (roomInfo.getRoomId() != null) {
                        updateHouseRoomInformation(roomInfo);
                        message.append("行 " + (roomInformationList.indexOf(roomInfo) + 1) + ": 更新成功\n");
                    } else {
                        insertHouseRoomInformation(roomInfo);
                        message.append("行 " + (roomInformationList.indexOf(roomInfo) + 1) + ": 插入成功\n");
                    }
                } else {
                    insertHouseRoomInformation(roomInfo);
                    message.append("行 " + (roomInformationList.indexOf(roomInfo) + 1) + ": 插入成功\n");
                }
            } catch (Exception e) {
                message.append("行 " + (roomInformationList.indexOf(roomInfo) + 1) + ": 操作失败 - " + e.getMessage() + "\n");
            }
        }

        return message.toString();
    }


    /**
     * 查询存储房间的基本信息，包括与房产的关联列表
     * 
     * @param houseRoomInformation 存储房间的基本信息，包括与房产的关联
     * @return 存储房间的基本信息，包括与房产的关联
     */
    @Override
    public List<HouseRoomInformation> selectHouseRoomInformationList(HouseRoomInformation houseRoomInformation) {
        List<HouseRoomInformation> houseRoomInformations = houseRoomInformationMapper.selectHouseRoomInformationList(houseRoomInformation);
        return houseRoomInformations;
    }

    @Override
    public List<HouseRoomInformation> selectHouseRoomInformationIdleList() {
        List<HouseRoomInformation> houseRoomInformations = houseRoomInformationMapper.selectHouseRoomInformationIdleList();
        return houseRoomInformations;
    }


    /**
     * 新增存储房间的基本信息，包括与房产的关联
     * 
     * @param houseRoomInformation 存储房间的基本信息，包括与房产的关联
     * @return 结果
     */
    @Override
    public int insertHouseRoomInformation(HouseRoomInformation houseRoomInformation)
    {
        houseRoomInformation.setPropertyName(housePropertyMapper.selectHousePropertyByPropertyId(houseRoomInformation.getPropertyId()).getPropertyName());
        houseRoomInformation.setCreatedAt(new Date());
        return houseRoomInformationMapper.insertHouseRoomInformation(houseRoomInformation);
    }

    /**
     * 修改存储房间的基本信息，包括与房产的关联
     * 
     * @param houseRoomInformation 存储房间的基本信息，包括与房产的关联
     * @return 结果
     */
    @Override
    public int updateHouseRoomInformation(HouseRoomInformation houseRoomInformation)
    {
        if (houseRoomInformation.getPropertyId()!=null){
            houseRoomInformation.setPropertyId(null);
        }
        return houseRoomInformationMapper.updateHouseRoomInformation(houseRoomInformation);
    }

    /**
     * 批量删除存储房间的基本信息，包括与房产的关联
     * 
     * @param roomIds 需要删除的存储房间的基本信息，包括与房产的关联主键
     * @return 结果
     */
    @Override
    public int deleteHouseRoomInformationByRoomIds(String[] roomIds)
    {
        return houseRoomInformationMapper.deleteHouseRoomInformationByRoomIds(roomIds);
    }

    /**
     * 删除存储房间的基本信息，包括与房产的关联信息
     * 
     * @param roomId 存储房间的基本信息，包括与房产的关联主键
     * @return 结果
     */
    @Override
    public int deleteHouseRoomInformationByRoomId(String roomId)
    {
        return houseRoomInformationMapper.deleteHouseRoomInformationByRoomId(roomId);
    }


    public String selectPropertyName(Long propertyId){
      return   housePropertyMapper.selectHousePropertyByPropertyId(propertyId).getPropertyName();
    }
}
